package com.google.code.sharecontent.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.google.code.sharecontent.dao.BaseDao;
import com.google.code.sharecontent.dao.PlaylistDao;
import com.google.code.sharecontent.model.Content;
import com.google.code.sharecontent.model.Playlist;

public class PlaylistDaoImpl  extends BaseDaoImpl<Playlist> implements PlaylistDao {

	public PlaylistDaoImpl(Connection conn) {
		super(conn);
	}

	@Override
	public Playlist createByRow(ResultSet rs) throws SQLException {
		Playlist playlist = new Playlist();
		playlist.setId(rs.getInt("ID"));
		playlist.setUserId(rs.getInt("USER_ID"));
		playlist.setStatusId(rs.getInt("STATUS_ID"));
		playlist.setName(rs.getString("NAME"));
		playlist.setDescription(rs.getString("DESCRIPTION"));
		playlist.setSortType(rs.getInt("SORT"));
		playlist.setPublicPlaylist( intToBoolean(rs.getInt("PIBLIC_PLAYLIST")) );
		return playlist;
	}
	
	@Override
	protected String generateInsertSql(Playlist playlist) {
		return "insert into "+getTableName()+" (USER_ID,STATUS_ID,NAME,DESCRIPTION,SORT,PIBLIC_PLAYLIST) " +
				"values (?,?,?,?,?,?)";
/*		return "insert into "+getTableName()+" (USER_ID,STATUS_ID,NAME,DESCRIPTION,SORT,PIBLIC_PLAYLIST) values (" +
		""+playlist.getUserId()+","+playlist.getStatusId()+","+getStringWithQuotesOrNull(playlist.getName())+","+
		getStringWithQuotesOrNull(playlist.getDescription())+","+playlist.getSortType()+"," +
		""+booleanToInt(playlist.getPublicPlaylist())+")";*/
	}
	
	@Override
	protected String generateUpdateSql(Playlist status) {
		return "update "+getTableName()+" set " +
				"USER_ID = ?," +
				"STATUS_ID = ?," +
				"NAME = ?," +
				"DESCRIPTION = ?," +
				"SORT = ?," +
				"PIBLIC_PLAYLIST = ? " +
		" where ID = "+ status.getId();
/*		return "update "+getTableName()+" set " +
		"USER_ID = "+status.getUserId()+"," +
		"STATUS_ID = "+status.getStatusId()+"," +
		"NAME = "+getStringWithQuotesOrNull(status.getName())+"," +
		"DESCRIPTION = "+getStringWithQuotesOrNull(status.getDescription())+"," +
		"SORT = "+status.getSortType()+"," +
		"PIBLIC_PLAYLIST = "+ booleanToInt(status.getPublicPlaylist())+" " +
" where ID = "+ status.getId();*/
	}
	
	@Override
	protected String getTableName() {
		return "PLAYLISTS";
	}

	@Override
	protected PreparedStatement putPrepareStatement(PreparedStatement pStat,
			Playlist status) throws SQLException {
			pStat.setInt(1, status.getUserId());
			pStat.setInt(2, status.getStatusId());
			pStat.setString(3, status.getName());
			pStat.setString(4, status.getDescription());
			pStat.setInt(5, status.getSortType());
			pStat.setInt(6, booleanToInt(status.getPublicPlaylist()));
		return pStat;
	}

}
